package com.zzyl.framework.interceptor;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * Mybatis-Plus 自动填充公共字段
 *
 * @Author: Zhy
 * @Date: 2025-02-22
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

	@Override
	public void insertFill(MetaObject metaObject) {
		log.info("开始插入填充...");

		this.strictInsertFill(metaObject, "createTime", Date.class, DateUtils.getNowDate());
		this.strictInsertFill(metaObject, "createBy", String.class, SecurityUtils.getUserId().toString());
	}

	@Override
	public void updateFill(MetaObject metaObject) {
		log.info("开始更新填充...");

		// System.out.println("updateTime: " + metaObject.getValue("updateTime"));

		this.setFieldValByName("updateTime", DateUtils.getNowDate(), metaObject);
		this.setFieldValByName("updateBy", SecurityUtils.getUserId().toString(), metaObject);
	}
}